package com.mk.settlement.service.test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    
    public static Date getMonday(String date)throws Exception{
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(date));
        int week = cal.get(Calendar.DAY_OF_WEEK);
        if(week>2){
            cal.add(Calendar.DAY_OF_MONTH,-(week-2)+7);
        }else{
            cal.add(Calendar.DAY_OF_MONTH,2-week+7);
        }
        return cal.getTime();
    }
    
    public static Date nextMonthFirstDate(String date) throws Exception{
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(date));
        
//        Calendar calendar = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.MONTH, 1);
        return cal.getTime();
    }
    
    public static String getMondayOfThisWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        return sdf.format(c.getTime());
    }

    public static void main(String[] args) throws Throwable {
        
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
       
//        Date date = sdf.parse("2015-12-02");
       
        
//        System.out.println(DateUtil.nextMonthFirstDate("2015-02-23"));
//        
//        System.out.println(DateUtil.getMonday("2016-02-23"));
//        
//        Order order = new Order();
//        order.setPrice(new BigDecimal(100.00));
//        order.setDate(new Date());
//        
//        System.out.println(JSON.toJSONString(order));
        
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(sdf.format(new Date()));
        
        System.out.println(DateUtil.getMondayOfThisWeek());
        
    }

}
